import { networkInterfaces } from "os";


class Utils {
    /**
     * 获取本机局域网IPv4地址
     * @returns 局域网IP地址（如 '192.168.1.100'）
     */
    static getLocalIP(): string {
        const interfaces = networkInterfaces();
        for (const devName in interfaces) {
            const iface = interfaces[devName];
            if (!iface) continue;

            for (const alias of iface) {
                if (
                    alias.family === 'IPv4' &&
                    alias.address !== '127.0.0.1' &&
                    !alias.internal
                ) {
                    return alias.address;
                }
            }
        }

        return '127.0.0.1';
    }

    /**
     * 格式化服务地址
     * @param ip IP地址
     * @param port 端口号
     * @returns 完整服务地址（如 'http://192.168.1.100:1300'）
     */
    static formatServiceUrl(ip: string, port: number): string {
        return `http://${ ip }:${ port }`;
    }

    /**
     * VITE_DEPEND_MODEL_NAME_LIST 配置 格式化成数组
     * app-shared,app-test
     * @param arg ["app-shared", "app-test"]
     */
    static dependModel(arg: string): string[] {
        return arg.split(',').map(name => name.trim()).filter(Boolean) || [];
    }
}

export const {
    getLocalIP, formatServiceUrl,
    dependModel
} = Utils